package edu.suda.xsap.controller;

import edu.suda.xsap.common.Result;
import edu.suda.xsap.dto.LogDto;
import edu.suda.xsap.service.LogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/log")
@Slf4j
@Tags({@Tag(name = "日志管理", description = "日志管理")})
public class LogController {

    @Autowired
    private LogService logService;

    @Operation(summary = "查询日志",description = "查询所有的日志信息")
    @ApiResponses ({
        @ApiResponse(responseCode = "200", description = "查询成功",
                content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                        schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class))),
            @ApiResponse(responseCode = "500", description = "查询失败",
                    content = @io.swagger.v3.oas.annotations.media.Content(mediaType = "application/json",
                            schema = @io.swagger.v3.oas.annotations.media.Schema(implementation = Result.class)))
    })
    @GetMapping("/list")
    public Result list() {
        //1. 参数处理
        //2. 调用业务层方法，并做异常处理
        try {
            List<LogDto> logDtos = logService.findAll();
            //
            return Result.success(logDtos);
        } catch (Exception e) {
            log.error("查询失败", e);
            return Result.failure(e.getMessage(), e);
        }
    }
}
